package com.localjoycommunity.cpdj.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.project.system.domain.SysNews;
import com.localjoycommunity.cpdj.project.system.domain.User;
import com.localjoycommunity.cpdj.project.system.domain.UserBeauty;
import com.localjoycommunity.cpdj.project.system.domain.UserNews;
import com.localjoycommunity.cpdj.project.system.mapper.UserMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserNewsMapper;
import com.localjoycommunity.cpdj.project.system.service.ISysNewsService;
import com.localjoycommunity.cpdj.project.system.service.IUserNewsService;
import com.localjoycommunity.cpdj.project.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author aqie
 * @date 2020-11-02 10:07
 * @function
 */
@Service
public class UserNewsServiceImpl extends ServiceImpl<UserNewsMapper, UserNews> implements IUserNewsService {
    // 0∧1＝1，1∧1＝0
    @Autowired
    private ISysNewsService sysNewsService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 新闻 点赞/喜欢
     *
     * @param userNews
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int newsDig(UserNews userNews) {
        // 1. 先查询
        UserNews one = baseMapper.selectOne(new QueryWrapper<UserNews>().lambda().eq(UserNews::getUserId, userNews.getUserId())
                .eq(UserNews::getNewsId, userNews.getNewsId()));
        if (one == null) {
            userNews.setIsDig(1);
            sysNewsService.updateDigNum(userNews.getNewsId(), 1);
            return baseMapper.insert(userNews);
        }
        User user = userMapper.selectUserById(userNews.getUserId());
        Long digNum = user.getDigNum() == null ? 0 : user.getDigNum();
        one.setIsDig(one.getIsDig() ^ 1);
        if (one.getIsDig() == 1) {
            sysNewsService.updateDigNum(userNews.getNewsId(), 1);
            user.setDigNum(digNum + 1);
        } else {
            digNum = digNum - 1 <= 0 ? 0 : digNum - 1;
            user.setDigNum(digNum);
            sysNewsService.updateDigNum(userNews.getNewsId(), -1);
        }
        userMapper.updateUser(user);
        return baseMapper.updateById(one);
    }

    /**
     * 点赞信息速览列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysNews> selectUserNews(Integer userId) {
        List<UserNews> userNews = baseMapper.selectList(new QueryWrapper<UserNews>().lambda().eq(UserNews::getUserId, userId).eq(UserNews::getIsDig, 1));
        if (userNews == null || userNews.size() == 0) {
            return null;
        }
        List<SysNews> list = new ArrayList<>();
        for (UserNews userNew : userNews) {
            SysNews sysNews = sysNewsService.selectSysNewsById(userNew.getNewsId());
            list.add(sysNews);
        }
        return list;
    }

    @Override
    public Integer isDig(int userId, Long newsId) {
        return baseMapper.isDig(userId, newsId).orElse(0);
    }
}
